<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【一行代码可以实现的事】字符串篇 | Vixcity</title><meta name="keywords" content="代码片段,JavaScript,一行代码,字符串"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【一行代码可以实现的事】字符串篇今天是国庆呀，祝我们伟大的祖国母亲生日快乐 (^▽^) 将字符串大写1234567const capitalize &#x3D; str &#x3D;&gt; &#96;$&amp;#123;str.charAt(0).toUpperCase()&amp;#125;$&amp;#123;str.slice(1)&amp;#125;&#96;;&#x2F;&#x2F; Orconst capitalize &#x3D; ([first, ...rest]) &#x3D;&amp;g">
<meta property="og:type" content="article">
<meta property="og:title" content="【一行代码可以实现的事】字符串篇">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="【一行代码可以实现的事】字符串篇今天是国庆呀，祝我们伟大的祖国母亲生日快乐 (^▽^) 将字符串大写1234567const capitalize &#x3D; str &#x3D;&gt; &#96;$&amp;#123;str.charAt(0).toUpperCase()&amp;#125;$&amp;#123;str.slice(1)&amp;#125;&#96;;&#x2F;&#x2F; Orconst capitalize &#x3D; ([first, ...rest]) &#x3D;&amp;g">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg">
<meta property="article:published_time" content="2021-09-30T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.329Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="代码片段">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="一行代码">
<meta property="article:tag" content="字符串">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【一行代码可以实现的事】字符串篇',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【一行代码可以实现的事】字符串篇</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-30T16:00:00.000Z" title="发表于 2021-10-01 00:00:00">2021-10-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.329Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81/">一行代码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【一行代码可以实现的事】字符串篇"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="【一行代码可以实现的事】字符串篇"><a href="#【一行代码可以实现的事】字符串篇" class="headerlink" title="【一行代码可以实现的事】字符串篇"></a>【一行代码可以实现的事】字符串篇</h2><p>今天是国庆呀，祝我们伟大的祖国母亲生日快乐 (<em>^▽^</em>)</p>
<h2 id="将字符串大写"><a href="#将字符串大写" class="headerlink" title="将字符串大写"></a>将字符串大写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> capitalize = <span class="function"><span class="params">str</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;str.charAt(<span class="number">0</span>).toUpperCase()&#125;</span><span class="subst">$&#123;str.slice(<span class="number">1</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> capitalize = <span class="function">(<span class="params">[first, ...rest]</span>) =&gt;</span> <span class="string">`<span class="subst">$&#123;first.toUpperCase()&#125;</span><span class="subst">$&#123;rest.join(<span class="string">&#x27;&#x27;</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">capitalize(<span class="string">&#x27;hello world&#x27;</span>);      <span class="comment">// &#x27;Hello world&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="检查路径是否是相对的"><a href="#检查路径是否是相对的" class="headerlink" title="检查路径是否是相对的"></a>检查路径是否是相对的</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> isRelative = <span class="function"><span class="params">path</span> =&gt;</span> !<span class="regexp">/^([a-z]+:)?[\\/]/i</span>.test(path);</span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">isRelative(<span class="string">&#x27;/foo/bar/baz&#x27;</span>);         <span class="comment">// false</span></span><br><span class="line">isRelative(<span class="string">&#x27;C:\\foo\\bar\\baz&#x27;</span>);    <span class="comment">// false</span></span><br><span class="line">isRelative(<span class="string">&#x27;foo/bar/baz.txt&#x27;</span>);      <span class="comment">// true</span></span><br><span class="line">isRelative(<span class="string">&#x27;foo.md&#x27;</span>);               <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="检查字符串是否由重复的字符序列组成"><a href="#检查字符串是否由重复的字符序列组成" class="headerlink" title="检查字符串是否由重复的字符序列组成"></a>检查字符串是否由重复的字符序列组成</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> consistsRepeatedSubstring = <span class="function"><span class="params">str</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;str&#125;</span><span class="subst">$&#123;str&#125;</span>`</span>.indexOf(str, <span class="number">1</span>) !== str.length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">consistsRepeatedSubstring(<span class="string">&#x27;aa&#x27;</span>);            <span class="comment">// true</span></span><br><span class="line">consistsRepeatedSubstring(<span class="string">&#x27;aaa&#x27;</span>);           <span class="comment">// true</span></span><br><span class="line">consistsRepeatedSubstring(<span class="string">&#x27;ababab&#x27;</span>);        <span class="comment">// true</span></span><br><span class="line">consistsRepeatedSubstring(<span class="string">&#x27;abc&#x27;</span>);           <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="检查字符串是否为回文"><a href="#检查字符串是否为回文" class="headerlink" title="检查字符串是否为回文"></a>检查字符串是否为回文</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> isPalindrome = <span class="function"><span class="params">str</span> =&gt;</span> str === str.split(<span class="string">&#x27;&#x27;</span>).reverse().join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">isPalindrome(<span class="string">&#x27;abc&#x27;</span>);        <span class="comment">// false</span></span><br><span class="line">isPalindrom(<span class="string">&#x27;abcba&#x27;</span>);       <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="检查一个-URL-是否是绝对的"><a href="#检查一个-URL-是否是绝对的" class="headerlink" title="检查一个 URL 是否是绝对的"></a>检查一个 URL 是否是绝对的</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> isAbsoluteUrl = <span class="function"><span class="params">url</span> =&gt;</span> <span class="regexp">/^[a-z][a-z0-9+.-]*:/</span>.test(url);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">isAbsoluteUrl(<span class="string">&#x27;https://1loc.dev&#x27;</span>);          <span class="comment">// true</span></span><br><span class="line">isAbsoluteUrl(<span class="string">&#x27;https://1loc.dev/foo/bar&#x27;</span>);  <span class="comment">// true</span></span><br><span class="line">isAbsoluteUrl(<span class="string">&#x27;1loc.dev&#x27;</span>);                  <span class="comment">// false</span></span><br><span class="line">isAbsoluteUrl(<span class="string">&#x27;//1loc.dev&#x27;</span>);                <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="检查两个字符串是否是字谜"><a href="#检查两个字符串是否是字谜" class="headerlink" title="检查两个字符串是否是字谜"></a>检查两个字符串是否是字谜</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> areAnagram = <span class="function">(<span class="params">str1, str2</span>) =&gt;</span> str1.toLowerCase().split(<span class="string">&#x27;&#x27;</span>).sort().join(<span class="string">&#x27;&#x27;</span>) === str2.toLowerCase().split(<span class="string">&#x27;&#x27;</span>).sort().join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">areAnagram(<span class="string">&#x27;listen&#x27;</span>, <span class="string">&#x27;silent&#x27;</span>);         <span class="comment">// true</span></span><br><span class="line">areAnagram(<span class="string">&#x27;they see&#x27;</span>, <span class="string">&#x27;the eyes&#x27;</span>);     <span class="comment">// true</span></span><br><span class="line">areAnagram(<span class="string">&#x27;node&#x27;</span>, <span class="string">&#x27;deno&#x27;</span>);             <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="将字母转换为关联表情符号"><a href="#将字母转换为关联表情符号" class="headerlink" title="将字母转换为关联表情符号"></a>将字母转换为关联表情符号</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> letterToEmoji = <span class="function"><span class="params">c</span> =&gt;</span> <span class="built_in">String</span>.fromCodePoint(c.toLowerCase().charCodeAt() + <span class="number">127365</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">letterToEmoji(<span class="string">&#x27;a&#x27;</span>);     <span class="comment">// 🇦</span></span><br><span class="line">letterToEmoji(<span class="string">&#x27;b&#x27;</span>);     <span class="comment">// 🇧</span></span><br></pre></td></tr></table></figure>

<h2 id="将字符串转换为驼峰式大小写"><a href="#将字符串转换为驼峰式大小写" class="headerlink" title="将字符串转换为驼峰式大小写"></a>将字符串转换为驼峰式大小写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> toCamelCase = <span class="function"><span class="params">str</span> =&gt;</span> str.trim().replace(<span class="regexp">/[-_\s]+(.)?/g</span>, <span class="function">(<span class="params">_, c</span>) =&gt;</span> c ? c.toUpperCase() : <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">toCamelCase(<span class="string">&#x27;background-color&#x27;</span>);            <span class="comment">// backgroundColor</span></span><br><span class="line">toCamelCase(<span class="string">&#x27;-webkit-scrollbar-thumb&#x27;</span>);     <span class="comment">// WebkitScrollbarThumb</span></span><br><span class="line">toCamelCase(<span class="string">&#x27;_hello_world&#x27;</span>);                <span class="comment">// HelloWorld</span></span><br><span class="line">toCamelCase(<span class="string">&#x27;hello_world&#x27;</span>);                 <span class="comment">// helloWorld</span></span><br></pre></td></tr></table></figure>

<h2 id="将字符串转换为-PascalCase"><a href="#将字符串转换为-PascalCase" class="headerlink" title="将字符串转换为 PascalCase"></a>将字符串转换为 PascalCase</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> toPascalCase = <span class="function"><span class="params">str</span> =&gt;</span> (str.match(<span class="regexp">/[a-zA-Z0-9]+/g</span>) || []).map(<span class="function"><span class="params">w</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;w.charAt(<span class="number">0</span>).toUpperCase()&#125;</span><span class="subst">$&#123;w.slice(<span class="number">1</span>)&#125;</span>`</span>).join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">toPascalCase(<span class="string">&#x27;hello world&#x27;</span>);    <span class="comment">// &#x27;HelloWorld&#x27;</span></span><br><span class="line">toPascalCase(<span class="string">&#x27;hello.world&#x27;</span>);    <span class="comment">// &#x27;HelloWorld&#x27;</span></span><br><span class="line">toPascalCase(<span class="string">&#x27;foo_bar-baz&#x27;</span>);    <span class="comment">// FooBarBaz</span></span><br></pre></td></tr></table></figure>

<h2 id="将字符串转换为-URL-slug"><a href="#将字符串转换为-URL-slug" class="headerlink" title="将字符串转换为 URL slug"></a>将字符串转换为 URL slug</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> slugify = <span class="function"><span class="params">string</span> =&gt;</span> string.toLowerCase().replace(<span class="regexp">/\s+/g</span>, <span class="string">&#x27;-&#x27;</span>).replace(<span class="regexp">/[^\w-]+/g</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">slugify(<span class="string">&#x27;Chapter One: Once upon a time...&#x27;</span>);    <span class="comment">// &#x27;chapter-one-once-upon-a-time&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="将-Windows-文件路径转换为-Unix-路径"><a href="#将-Windows-文件路径转换为-Unix-路径" class="headerlink" title="将 Windows 文件路径转换为 Unix 路径"></a>将 Windows 文件路径转换为 Unix 路径</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> toUnixPath = <span class="function"><span class="params">path</span> =&gt;</span> path.replace(<span class="regexp">/[\\/]+/g</span>, <span class="string">&#x27;/&#x27;</span>).replace(<span class="regexp">/^([a-zA-Z]+:|\.\/)/</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">toUnixPath(<span class="string">&#x27;./foo/bar/baz&#x27;</span>);        <span class="comment">// foo/bar/baz</span></span><br><span class="line">toUnixPath(<span class="string">&#x27;C:\\foo\\bar\\baz&#x27;</span>);    <span class="comment">// /foo/bar/baz</span></span><br></pre></td></tr></table></figure>

<h2 id="将camelCase-转换为kebab-case，反之亦然"><a href="#将camelCase-转换为kebab-case，反之亦然" class="headerlink" title="将camelCase 转换为kebab-case，反之亦然"></a>将camelCase 转换为kebab-case，反之亦然</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> kebabToCamel = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/-./g</span>, <span class="function"><span class="params">m</span> =&gt;</span> m.toUpperCase()[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> camelToKebab = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/([a-z0-9])([A-Z])/g</span>, <span class="string">&#x27;$1-$2&#x27;</span>).toLowerCase();</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">kebabToCamel(<span class="string">&#x27;background-color&#x27;</span>);   <span class="comment">// &#x27;backgroundColor&#x27;</span></span><br><span class="line">camelToKebab(<span class="string">&#x27;backgroundColor&#x27;</span>);    <span class="comment">// &#x27;background-color&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="将snake-case-转换为camelCase"><a href="#将snake-case-转换为camelCase" class="headerlink" title="将snake_case 转换为camelCase"></a>将snake_case 转换为camelCase</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> snakeToCamel = <span class="function"><span class="params">str</span> =&gt;</span> str.toLowerCase().replace(<span class="regexp">/(_\w)/g</span>, <span class="function"><span class="params">m</span> =&gt;</span> m.toUpperCase().substr(<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">snakeToCamel(<span class="string">&#x27;HELLO_world&#x27;</span>);    <span class="comment">// &#x27;helloWorld&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="将-Excel-列的名称转换为数字"><a href="#将-Excel-列的名称转换为数字" class="headerlink" title="将 Excel 列的名称转换为数字"></a>将 Excel 列的名称转换为数字</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getIndex = <span class="function"><span class="params">col</span> =&gt;</span> col.split(<span class="string">&#x27;&#x27;</span>).reduce(<span class="function">(<span class="params">prev, next</span>) =&gt;</span> prev * <span class="number">26</span> + <span class="built_in">parseInt</span>(next, <span class="number">36</span>) - <span class="number">9</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">getIndex(<span class="string">&#x27;A&#x27;</span>);      <span class="comment">// 1</span></span><br><span class="line">getIndex(<span class="string">&#x27;B&#x27;</span>);      <span class="comment">// 2</span></span><br><span class="line">getIndex(<span class="string">&#x27;C&#x27;</span>);      <span class="comment">// 3</span></span><br><span class="line">getIndex(<span class="string">&#x27;Z&#x27;</span>);      <span class="comment">// 26</span></span><br><span class="line">getIndex(<span class="string">&#x27;AA&#x27;</span>);     <span class="comment">// 27</span></span><br><span class="line">getIndex(<span class="string">&#x27;AB&#x27;</span>);     <span class="comment">// 28</span></span><br><span class="line">getIndex(<span class="string">&#x27;AC&#x27;</span>);     <span class="comment">// 29</span></span><br><span class="line">getIndex(<span class="string">&#x27;AZ&#x27;</span>);     <span class="comment">// 52</span></span><br><span class="line">getIndex(<span class="string">&#x27;AAA&#x27;</span>);    <span class="comment">// 703</span></span><br><span class="line">getIndex(<span class="string">&#x27;AAB&#x27;</span>);    <span class="comment">// 704</span></span><br></pre></td></tr></table></figure>

<h2 id="计算字符串中的单词数"><a href="#计算字符串中的单词数" class="headerlink" title="计算字符串中的单词数"></a>计算字符串中的单词数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countWords = <span class="function"><span class="params">str</span> =&gt;</span> str.trim().split(<span class="regexp">/\s+/</span>).length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">countWords(<span class="string">&#x27;Hello World&#x27;</span>);        <span class="comment">// 2</span></span><br><span class="line">countWords(<span class="string">&#x27;Hello    World&#x27;</span>);     <span class="comment">// 2</span></span><br><span class="line">countWords(<span class="string">&#x27;  Hello  World  &#x27;</span>);   <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="计算字符串中某个字符的出现次数"><a href="#计算字符串中某个字符的出现次数" class="headerlink" title="计算字符串中某个字符的出现次数"></a>计算字符串中某个字符的出现次数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">str, char</span>) =&gt;</span> [...str].reduce(<span class="function">(<span class="params">a, v</span>) =&gt;</span> (v === char ? a + <span class="number">1</span> : a), <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">str, char</span>) =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).reduce(<span class="function">(<span class="params">a, v</span>) =&gt;</span> (v === char ? a + <span class="number">1</span> : a), <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">str, char</span>) =&gt;</span> [...str].filter(<span class="function"><span class="params">item</span> =&gt;</span> item === char).length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">str, char</span>) =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).filter(<span class="function"><span class="params">item</span> =&gt;</span> item === char).length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ExamplescountOccurrences(&#x27;a.b.c.d.e&#x27;, &#x27;.&#x27;);     // 4</span></span><br></pre></td></tr></table></figure>

<h2 id="对字符串进行大写"><a href="#对字符串进行大写" class="headerlink" title="对字符串进行大写"></a>对字符串进行大写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> decapitalize = <span class="function"><span class="params">str</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;str.charAt(<span class="number">0</span>).toLowerCase()&#125;</span><span class="subst">$&#123;str.slice(<span class="number">1</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> decapitalize = <span class="function">(<span class="params">[first, ...rest]</span>) =&gt;</span> <span class="string">`<span class="subst">$&#123;first.toLowerCase()&#125;</span><span class="subst">$&#123;rest.join(<span class="string">&#x27;&#x27;</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">decapitalize(<span class="string">&#x27;Hello world&#x27;</span>);    <span class="comment">// &#x27;hello world&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="转义-HTML-特殊字符"><a href="#转义-HTML-特殊字符" class="headerlink" title="转义 HTML 特殊字符"></a>转义 HTML 特殊字符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="built_in">escape</span> = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/&amp;/g</span>, <span class="string">&#x27;&amp;amp;&#x27;</span>).replace(<span class="regexp">/&lt;/g</span>, <span class="string">&#x27;&amp;lt;&#x27;</span>).replace(<span class="regexp">/&gt;/g</span>, <span class="string">&#x27;&amp;gt;&#x27;</span>).replace(<span class="regexp">/&#x27;/g</span>, <span class="string">&#x27;&amp;#39;&#x27;</span>).replace(<span class="regexp">/&quot;/g</span>, <span class="string">&#x27;&amp;quot;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> <span class="built_in">escape</span> = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/[&amp;&lt;&gt;&quot;&#x27;]/g</span>, <span class="function"><span class="params">m</span> =&gt;</span> (&#123; <span class="string">&#x27;&amp;&#x27;</span>: <span class="string">&#x27;&amp;amp;&#x27;</span>, <span class="string">&#x27;&lt;&#x27;</span>: <span class="string">&#x27;&amp;lt;&#x27;</span>, <span class="string">&#x27;&gt;&#x27;</span>: <span class="string">&#x27;&amp;gt;&#x27;</span>, <span class="string">&#x27;&quot;&#x27;</span>: <span class="string">&#x27;&amp;quot;&#x27;</span>, <span class="string">&quot;&#x27;&quot;</span>: <span class="string">&#x27;&amp;#39;&#x27;</span> &#125;)[m]);</span><br></pre></td></tr></table></figure>

<h2 id="生成一个字符串的哈希"><a href="#生成一个字符串的哈希" class="headerlink" title="生成一个字符串的哈希"></a>生成一个字符串的哈希</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> hash = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> <span class="built_in">Math</span>.imul(<span class="number">31</span>, prev) + curr.charCodeAt(<span class="number">0</span>) | <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> hash = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> (((prev &lt;&lt; <span class="number">5</span>) - prev) + curr.charCodeAt(<span class="number">0</span>)) | <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">hash(<span class="string">&#x27;hello&#x27;</span>);      <span class="comment">// 99162322</span></span><br></pre></td></tr></table></figure>

<h2 id="获取不带任何参数的基本-URL"><a href="#获取不带任何参数的基本-URL" class="headerlink" title="获取不带任何参数的基本 URL"></a>获取不带任何参数的基本 URL</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> baseUrl = <span class="function"><span class="params">url</span> =&gt;</span> url.indexOf(<span class="string">&#x27;?&#x27;</span>) === -<span class="number">1</span> ? url : url.slice(<span class="number">0</span>, url.indexOf(<span class="string">&#x27;?&#x27;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="comment">// Note that `includes` isn&#x27;t supported in IE 11</span></span><br><span class="line"><span class="keyword">const</span> baseUrl = <span class="function"><span class="params">url</span> =&gt;</span> url.includes(<span class="string">&#x27;?&#x27;</span>) ? url.slice(<span class="number">0</span>, url.indexOf(<span class="string">&#x27;?&#x27;</span>)) : url;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">baseUrl(<span class="string">&#x27;https://domain.com/path/sub/path?foo=bar&amp;hello=world&#x27;</span>);    <span class="comment">// &#x27;https://domain.com/path/sub/path&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="从文件名中获取文件扩展名"><a href="#从文件名中获取文件扩展名" class="headerlink" title="从文件名中获取文件扩展名"></a>从文件名中获取文件扩展名</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ext = <span class="function"><span class="params">fileName</span> =&gt;</span> fileName.split(<span class="string">&#x27;.&#x27;</span>).pop();</span><br></pre></td></tr></table></figure>

<h2 id="从-URL-获取文件名"><a href="#从-URL-获取文件名" class="headerlink" title="从 URL 获取文件名"></a>从 URL 获取文件名</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fileName = <span class="function"><span class="params">url</span> =&gt;</span> url.substring(url.lastIndexOf(<span class="string">&#x27;/&#x27;</span>) + <span class="number">1</span>);</span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">fileName(<span class="string">&#x27;http://domain.com/path/to/document.pdf&#x27;</span>);     <span class="comment">// &#x27;document.pdf&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="获取字符串的长度（以字节为单位）"><a href="#获取字符串的长度（以字节为单位）" class="headerlink" title="获取字符串的长度（以字节为单位）"></a>获取字符串的长度（以字节为单位）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> bytes = <span class="function"><span class="params">str</span> =&gt;</span> <span class="keyword">new</span> Blob([str]).size;</span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">bytes(<span class="string">&#x27;hello world&#x27;</span>);       <span class="comment">// 11</span></span><br><span class="line">bytes(<span class="string">&#x27;🎉&#x27;</span>);                <span class="comment">// 4</span></span><br></pre></td></tr></table></figure>

<h2 id="获取字符串中某个字符的个数"><a href="#获取字符串中某个字符的个数" class="headerlink" title="获取字符串中某个字符的个数"></a>获取字符串中某个字符的个数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> characterCount = <span class="function">(<span class="params">str, char</span>) =&gt;</span> str.split(char).length - <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> characterCount = <span class="function">(<span class="params">str, char</span>) =&gt;</span> str.replace((<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="built_in">String</span>.raw<span class="string">`[^<span class="subst">$&#123;char&#125;</span>]`</span>, <span class="string">&#x27;g&#x27;</span>)), <span class="string">&#x27;&#x27;</span>).length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">characterCount(<span class="string">&#x27;192.168.1.1&#x27;</span>, <span class="string">&#x27;.&#x27;</span>);     <span class="comment">// 3</span></span><br><span class="line">characterCount(<span class="string">&#x27;star wars&#x27;</span>, <span class="string">&#x27;s&#x27;</span>);       <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="使字符串的第一个字符小写"><a href="#使字符串的第一个字符小写" class="headerlink" title="使字符串的第一个字符小写"></a>使字符串的第一个字符小写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> lowercaseFirst = <span class="function"><span class="params">str</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;str.charAt(<span class="number">0</span>).toLowerCase()&#125;</span><span class="subst">$&#123;str.slice(<span class="number">1</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">lowercaseFirst(<span class="string">&#x27;Hello World&#x27;</span>);      <span class="comment">// &#x27;hello World&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="规范化文件路径斜线"><a href="#规范化文件路径斜线" class="headerlink" title="规范化文件路径斜线"></a>规范化文件路径斜线</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> normalizePath = <span class="function"><span class="params">path</span> =&gt;</span> path.replace(<span class="regexp">/[\\/]+/g</span>, <span class="string">&#x27;/&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">normalizePath(<span class="string">&#x27;\\foo\\bar\\baz\\&#x27;</span>);         <span class="comment">// /foo/bar/baz/</span></span><br><span class="line">normalizePath(<span class="string">&#x27;.//foo//bar///////baz/&#x27;</span>);    <span class="comment">// ./foo/bar/baz/</span></span><br></pre></td></tr></table></figure>

<h2 id="为文本文档的每一行添加一个行号"><a href="#为文本文档的每一行添加一个行号" class="headerlink" title="为文本文档的每一行添加一个行号"></a>为文本文档的每一行添加一个行号</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> prependNumbers = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="regexp">/\r?\n/</span>).map(<span class="function">(<span class="params">line, i</span>) =&gt;</span> <span class="string">`<span class="subst">$&#123;(i + <span class="number">1</span>).toString().padStart(<span class="number">2</span>, <span class="string">&#x27; &#x27;</span>)&#125;</span> <span class="subst">$&#123;line&#125;</span>`</span>).join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">prependNumbers(<span class="string">`</span></span><br><span class="line"><span class="string">one</span></span><br><span class="line"><span class="string">two</span></span><br><span class="line"><span class="string">three</span></span><br><span class="line"><span class="string">four`</span>);</span><br><span class="line"> </span><br><span class="line"><span class="comment">/* Output */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1 one</span></span><br><span class="line"><span class="comment">2 two</span></span><br><span class="line"><span class="comment">3 three</span></span><br><span class="line"><span class="comment">4 four</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="删除文本文档的重复行"><a href="#删除文本文档的重复行" class="headerlink" title="删除文本文档的重复行"></a>删除文本文档的重复行</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> removeDuplicateLines = <span class="function"><span class="params">str</span> =&gt;</span> <span class="built_in">Array</span>.from(<span class="keyword">new</span> <span class="built_in">Set</span>(str.split(<span class="regexp">/\r?\n/</span>))).join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">removeDuplicateLines(<span class="string">`</span></span><br><span class="line"><span class="string">one</span></span><br><span class="line"><span class="string">three</span></span><br><span class="line"><span class="string">two</span></span><br><span class="line"><span class="string">three</span></span><br><span class="line"><span class="string">one</span></span><br><span class="line"><span class="string">four`</span>);</span><br><span class="line"> </span><br><span class="line"><span class="comment">/* Output */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">one</span></span><br><span class="line"><span class="comment">three</span></span><br><span class="line"><span class="comment">two</span></span><br><span class="line"><span class="comment">four</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="删除文本文档的空行"><a href="#删除文本文档的空行" class="headerlink" title="删除文本文档的空行"></a>删除文本文档的空行</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> removeEmptyLines = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="regexp">/\r?\n/</span>).filter(<span class="function"><span class="params">line</span> =&gt;</span> line.trim() !== <span class="string">&#x27;&#x27;</span>).join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">removeEmptyLines(<span class="string">`</span></span><br><span class="line"><span class="string">red</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">green</span></span><br><span class="line"><span class="string">blue</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">yellow`</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* Output */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">red</span></span><br><span class="line"><span class="comment">green</span></span><br><span class="line"><span class="comment">blue</span></span><br><span class="line"><span class="comment">yellow</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="从字符串中删除空格"><a href="#从字符串中删除空格" class="headerlink" title="从字符串中删除空格"></a>从字符串中删除空格</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> removeSpaces = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/\s/g</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">removeSpaces(<span class="string">&#x27;hel lo wor ld&#x27;</span>);      <span class="comment">// &#x27;helloworld&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="重复一个字符串"><a href="#重复一个字符串" class="headerlink" title="重复一个字符串"></a>重复一个字符串</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> repeat = <span class="function">(<span class="params">str, numberOfTimes</span>) =&gt;</span> str.repeat(numberOfTimes);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> repeat = <span class="function">(<span class="params">str, numberOfTimes</span>) =&gt;</span> <span class="built_in">Array</span>(numberOfTimes + <span class="number">1</span>).join(str);</span><br></pre></td></tr></table></figure>

<h2 id="用-br-元素替换所有换行符"><a href="#用-br-元素替换所有换行符" class="headerlink" title="用 br 元素替换所有换行符"></a>用 br 元素替换所有换行符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> nl2br = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;\r?\n&#x27;</span>, <span class="string">&#x27;g&#x27;</span>), <span class="string">&#x27;&lt;br&gt;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// In React</span></span><br><span class="line">str.split(<span class="string">&#x27;\n&#x27;</span>).map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> <span class="xml"><span class="tag">&lt;<span class="name">React.Fragment</span> <span class="attr">key</span>=<span class="string">&#123;index&#125;</span>&gt;</span>&#123;item&#125;<span class="tag">&lt;<span class="name">br</span> /&gt;</span><span class="tag">&lt;/<span class="name">React.Fragment</span>&gt;</span></span>)</span><br></pre></td></tr></table></figure>

<h2 id="用空格替换所有制表符"><a href="#用空格替换所有制表符" class="headerlink" title="用空格替换所有制表符"></a>用空格替换所有制表符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> replace = <span class="function">(<span class="params">str, numSpaces = <span class="number">4</span></span>) =&gt;</span> str.replaceAll(<span class="string">&#x27;\t&#x27;</span>, <span class="string">&#x27; &#x27;</span>.repeat(numSpaces));</span><br></pre></td></tr></table></figure>

<h2 id="用一个空格替换多个空格"><a href="#用一个空格替换多个空格" class="headerlink" title="用一个空格替换多个空格"></a>用一个空格替换多个空格</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Replace spaces, tabs and new line characters</span></span><br><span class="line"><span class="keyword">const</span> replaceSpaces = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/\s\s+/g</span>, <span class="string">&#x27; &#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Only replace spaces</span></span><br><span class="line"><span class="keyword">const</span> replaceOnlySpaces = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/  +/g</span>, <span class="string">&#x27; &#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">replaceSpaces(<span class="string">&#x27;this\n   is     \ta    \rmessage&#x27;</span>);  <span class="comment">// &#x27;this is a message&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="用另一个字符替换字符串的第一个给定字符数"><a href="#用另一个字符替换字符串的第一个给定字符数" class="headerlink" title="用另一个字符替换字符串的第一个给定字符数"></a>用另一个字符替换字符串的第一个给定字符数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mask = <span class="function">(<span class="params">str, num, mask</span>) =&gt;</span> <span class="string">`<span class="subst">$&#123;str&#125;</span>`</span>.slice(num).padStart(<span class="string">`<span class="subst">$&#123;str&#125;</span>`</span>.length, mask);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">mask(<span class="number">1234567890</span>, <span class="number">3</span>, <span class="string">&#x27;*&#x27;</span>);       <span class="comment">// ***4567890</span></span><br></pre></td></tr></table></figure>

<h2 id="反转字符串"><a href="#反转字符串" class="headerlink" title="反转字符串"></a>反转字符串</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> reverse = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).reverse().join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> reverse = <span class="function"><span class="params">str</span> =&gt;</span> [...str].reverse().join(<span class="string">&#x27;&#x27;</span>);<span class="comment">// Orconst reverse = str =&gt; str.split(&#x27;&#x27;).reduce((rev, char)=&gt; `$&#123;char&#125;$&#123;rev&#125;`, &#x27;&#x27;);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> reverse = <span class="function"><span class="params">str</span> =&gt;</span> (str === <span class="string">&#x27;&#x27;</span>) ? <span class="string">&#x27;&#x27;</span> : <span class="string">`<span class="subst">$&#123;reverse(str.substr(<span class="number">1</span>))&#125;</span><span class="subst">$&#123;str.charAt(<span class="number">0</span>)&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">reverse(<span class="string">&#x27;hello world&#x27;</span>);     <span class="comment">// &#x27;dlrow olleh&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="反转文本行的顺序"><a href="#反转文本行的顺序" class="headerlink" title="反转文本行的顺序"></a>反转文本行的顺序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> reverseLines = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="regexp">/\r?\n/</span>).reverse().join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">reverseLines(<span class="string">`</span></span><br><span class="line"><span class="string">one</span></span><br><span class="line"><span class="string">two</span></span><br><span class="line"><span class="string">three`</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* Output */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">three</span></span><br><span class="line"><span class="comment">two</span></span><br><span class="line"><span class="comment">one</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="按字母顺序对文本文档的行进行排序"><a href="#按字母顺序对文本文档的行进行排序" class="headerlink" title="按字母顺序对文本文档的行进行排序"></a>按字母顺序对文本文档的行进行排序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sortLines = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="regexp">/\r?\n/</span>).sort().join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Reverse the order</span></span><br><span class="line"><span class="keyword">const</span> reverseSortedLines = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="regexp">/\r?\n/</span>).sort().reverse().join(<span class="string">&#x27;\n&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">sortLines(<span class="string">`Thaddeus Mullen</span></span><br><span class="line"><span class="string">Kareem Marshall</span></span><br><span class="line"><span class="string">Ferdinand Valentine</span></span><br><span class="line"><span class="string">Hasad Lindsay</span></span><br><span class="line"><span class="string">Mufutau Berg</span></span><br><span class="line"><span class="string">Knox Tyson</span></span><br><span class="line"><span class="string">Kasimir Fletcher</span></span><br><span class="line"><span class="string">Colton Sharp</span></span><br><span class="line"><span class="string">Adrian Rosales</span></span><br><span class="line"><span class="string">Theodore Rogers`</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* Output */</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">Adrian Rosales</span></span><br><span class="line"><span class="comment">Colton Sharp</span></span><br><span class="line"><span class="comment">Ferdinand Valentine</span></span><br><span class="line"><span class="comment">Hasad Lindsay</span></span><br><span class="line"><span class="comment">Kareem Marshall</span></span><br><span class="line"><span class="comment">Kasimir Fletcher</span></span><br><span class="line"><span class="comment">Knox Tyson</span></span><br><span class="line"><span class="comment">Mufutau Berg</span></span><br><span class="line"><span class="comment">Thaddeus Mullen</span></span><br><span class="line"><span class="comment">Theodore Rogers</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="按字母顺序对字符串的字符进行排序"><a href="#按字母顺序对字符串的字符进行排序" class="headerlink" title="按字母顺序对字符串的字符进行排序"></a>按字母顺序对字符串的字符进行排序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sort = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a.localeCompare(b)).join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">sort(<span class="string">&#x27;hello world&#x27;</span>);    <span class="comment">// dehllloorw</span></span><br></pre></td></tr></table></figure>

<h2 id="从字符串中去除-ANSI-代码"><a href="#从字符串中去除-ANSI-代码" class="headerlink" title="从字符串中去除 ANSI 代码"></a>从字符串中去除 ANSI 代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> stripAnsiCodes = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/[\u001b\u009b][[()#;?]*(?:[0-9]&#123;1,4&#125;(?:;[0-9]&#123;0,4&#125;)*)?[0-9A-ORZcf-nqry=&gt;&lt;]/g</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">stripAnsiCodes(<span class="string">&#x27;\u001B[4mcake\u001B[0m&#x27;</span>);                                                               <span class="comment">// &#x27;cake&#x27;</span></span><br><span class="line">stripAnsiCodes(<span class="string">&#x27;\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m&#x27;</span>);  <span class="comment">// &#x27;foofoo&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="交换字符串中字符的大小写"><a href="#交换字符串中字符的大小写" class="headerlink" title="交换字符串中字符的大小写"></a>交换字符串中字符的大小写</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> swapCase = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;&#x27;</span>).map(<span class="function"><span class="params">c</span> =&gt;</span> (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase())).join(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">swapCase(<span class="string">&#x27;Hello World&#x27;</span>);      <span class="comment">// &#x27;hELLO wORLD&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="在字符串的开头和结尾修剪斜线"><a href="#在字符串的开头和结尾修剪斜线" class="headerlink" title="在字符串的开头和结尾修剪斜线"></a>在字符串的开头和结尾修剪斜线</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> trimSlashes = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/^\/+|\/+$/g</span>, <span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> trimSlashes = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27;/&#x27;</span>).filter(<span class="built_in">Boolean</span>).join(<span class="string">&#x27;/&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">trimSlashes(<span class="string">&#x27;//hello/world///&#x27;</span>);    <span class="comment">// hello/world</span></span><br></pre></td></tr></table></figure>

<h2 id="修剪一些字符"><a href="#修剪一些字符" class="headerlink" title="修剪一些字符"></a>修剪一些字符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> trim = <span class="function">(<span class="params">str, char</span>) =&gt;</span> str.split(char).filter(<span class="built_in">Boolean</span>).join();</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">trim(<span class="string">&#x27;/hello world//&#x27;</span>, <span class="string">&#x27;/&#x27;</span>);        <span class="comment">// hello world</span></span><br><span class="line">trim(<span class="string">&#x27;&quot;hello world&quot;&#x27;</span>, <span class="string">&#x27;&quot;&#x27;</span>);         <span class="comment">// hello world</span></span><br><span class="line">trim(<span class="string">&#x27;   hello world &#x27;</span>, <span class="string">&#x27; &#x27;</span>);       <span class="comment">// hello world</span></span><br></pre></td></tr></table></figure>

<h2 id="从文件名中修剪文件扩展名"><a href="#从文件名中修剪文件扩展名" class="headerlink" title="从文件名中修剪文件扩展名"></a>从文件名中修剪文件扩展名</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> trimExt = <span class="function"><span class="params">fileName</span> =&gt;</span> fileName.indexOf(<span class="string">&#x27;.&#x27;</span>) === -<span class="number">1</span> ? fileName : fileName.split(<span class="string">&#x27;.&#x27;</span>).slice(<span class="number">0</span>, -<span class="number">1</span>).join(<span class="string">&#x27;.&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">trimExt(<span class="string">&#x27;document&#x27;</span>);            <span class="comment">// document</span></span><br><span class="line">trimExt(<span class="string">&#x27;document.pdf&#x27;</span>);        <span class="comment">// document</span></span><br><span class="line">trimExt(<span class="string">&#x27;document.2020.pdf&#x27;</span>);   <span class="comment">// document.2020</span></span><br></pre></td></tr></table></figure>

<h2 id="在完整单词处截断字符串"><a href="#在完整单词处截断字符串" class="headerlink" title="在完整单词处截断字符串"></a>在完整单词处截断字符串</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> truncate = <span class="function">(<span class="params">str, max, suffix</span>) =&gt;</span> str.length &lt; max ? str : <span class="string">`<span class="subst">$&#123;str.substr(<span class="number">0</span>, str.substr(<span class="number">0</span>, max - suffix.length).lastIndexOf(<span class="string">&#x27; &#x27;</span>))&#125;</span><span class="subst">$&#123;suffix&#125;</span>`</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">truncate(<span class="string">&#x27;This is a long message&#x27;</span>, <span class="number">20</span>, <span class="string">&#x27;...&#x27;</span>);  <span class="comment">// &#x27;This is a long...&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="取消转义-HTML-特殊字符"><a href="#取消转义-HTML-特殊字符" class="headerlink" title="取消转义 HTML 特殊字符"></a>取消转义 HTML 特殊字符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="built_in">unescape</span> = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/&amp;amp;/g</span> , <span class="string">&#x27;&amp;&#x27;</span>).replace(<span class="regexp">/&amp;lt;/g</span>  , <span class="string">&#x27;&lt;&#x27;</span>).replace(<span class="regexp">/&amp;gt;/g</span>  , <span class="string">&#x27;&gt;&#x27;</span>).replace(<span class="regexp">/�*39;/g</span> , <span class="string">&quot;&#x27;&quot;</span>).replace(<span class="regexp">/&amp;quot;/g</span>, <span class="string">&#x27;&quot;&#x27;</span>);</span><br></pre></td></tr></table></figure>

<h2 id="大写字符串中每个单词的第一个字符"><a href="#大写字符串中每个单词的第一个字符" class="headerlink" title="大写字符串中每个单词的第一个字符"></a>大写字符串中每个单词的第一个字符</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> uppercaseWords = <span class="function"><span class="params">str</span> =&gt;</span> str.split(<span class="string">&#x27; &#x27;</span>).map(<span class="function"><span class="params">w</span> =&gt;</span> <span class="string">`<span class="subst">$&#123;w.charAt(<span class="number">0</span>).toUpperCase()&#125;</span><span class="subst">$&#123;w.slice(<span class="number">1</span>)&#125;</span>`</span>).join(<span class="string">&#x27; &#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> uppercaseWords = <span class="function"><span class="params">str</span> =&gt;</span> str.replace(<span class="regexp">/^(.)|\s+(.)/g</span>, <span class="function"><span class="params">c</span> =&gt;</span> c.toUpperCase());</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">uppercaseWords(<span class="string">&#x27;hello world&#x27;</span>);      <span class="comment">// &#x27;Hello World&#x27;</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">Vixcity</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/">http://vixcity.gitee.io/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://vixcity.gitee.io/my-gitee-blog" target="_blank">Vixcity</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/">代码片段</a><a class="post-meta__tags" href="/my-gitee-blog/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/my-gitee-blog/tags/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81/">一行代码</a><a class="post-meta__tags" href="/my-gitee-blog/tags/%E5%AD%97%E7%AC%A6%E4%B8%B2/">字符串</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/JS.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/10/03/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E9%AA%8C%E8%AF%81%E5%99%A8%E7%AF%87/"><img class="prev-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【一行代码可以实现的事】验证器篇</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E9%9A%8F%E6%9C%BA%E7%AF%87/"><img class="next-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【一行代码可以实现的事】随机篇</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/09/26/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91DOM%E8%8A%82%E7%82%B9%E7%AF%87/" title="【一行代码可以实现的事】DOM节点篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-26</div><div class="title">【一行代码可以实现的事】DOM节点篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/29/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%85%B6%E4%BB%96%E7%AF%87/" title="【一行代码可以实现的事】其他篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">【一行代码可以实现的事】其他篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/28/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%87%BD%E6%95%B0%E7%AF%87/" title="【一行代码可以实现的事】函数篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-28</div><div class="title">【一行代码可以实现的事】函数篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/30/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E5%AD%97%E7%AF%87/" title="【一行代码可以实现的事】数字篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-30</div><div class="title">【一行代码可以实现的事】数字篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/30/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AF%B9%E8%B1%A1%E7%AF%87/" title="【一行代码可以实现的事】对象篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-30</div><div class="title">【一行代码可以实现的事】对象篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/29/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E5%AD%A6%E7%AF%87/" title="【一行代码可以实现的事】数学篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">【一行代码可以实现的事】数学篇</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>